﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using UI.WebPage.Background;
using Model.AuthModel;
using BLL.AuthModel;
using Util.Configuration;
using Com.Ompchina.Lib.Text;
using Com.Ompchina.Lib.Net;
namespace Com.Ompchina.Web.admin.system
{
    public partial class add_staff : AddInfoPage, IAddInfo
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && PageAction.Equals("update")) { this.InitSingleInfo(this.GetUpdateID()); }
        }

        #region IAddPage 成员
        /// <summary>
        /// 检查输入
        /// </summary>
        /// <returns></returns>
        public bool CheckInput()
        {
            if (string.IsNullOrEmpty(txtUsername.Text))
            {
                base.SetMessage(false, "请输入用户名");
                return false;
            }
            else if (hfID.Value.Equals("0") && !chRandom.Checked && string.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                base.SetMessage(false, "创建成员时，必须输入密码或选择随机密码");
                return false;
            }
            else if (string.IsNullOrEmpty(txtEmail.Text.Trim()))
            {
                base.SetMessage(false, "请输入用户邮箱");
                return false;
            }
            else if (!new Regular().ReguEmail(txtEmail.Text.Trim()))
            {
                base.SetMessage(false, "邮箱格式错误，邮件无法送达");
                return false;
            }
            return true;
        }
        /// <summary>
        /// 初始化信息
        /// </summary>
        /// <param name="id"></param>
        public void InitSingleInfo(int id)
        {
            StaffInfo info = new StaffBLL().GetStaffByID(id);
            if (info.StaffID > 0)
            {
                hfID.Value = info.StaffID.ToString();
                txtUsername.Text = Server.HtmlDecode(info.Username);
                hfPassword.Value = info.Password;
                txtEmail.Text = Server.HtmlDecode(info.Email);
                chLock.Checked = info.IsLock.Equals("y");
                this.InitStaffRoles(info.StaffID);
            }
        }
        #endregion

        #region 页面私有方法
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="orgPass">密码明文</param>
        /// <param name="pass">密码密文</param>
        private void SendMail(string orgPass, string pass)
        {
            Mail mail = new Mail(WebConfiguration.MailSmtp, WebConfiguration.MailUsername, WebConfiguration.MailPassword);
            string html = "您的管理员帐号进行了" + (PageAction.Equals("add") ? "创建" : "修改") + "操作：<br />登录网址："
                + WebConfiguration.WebUri + WebConfiguration.AdminRoot + "<br />用户名：" + txtUsername.Text;
            if (string.IsNullOrEmpty(orgPass))
            {
                html += "<br />密码：密码未修改";
            }
            else
                html += "<br />密码为：" + orgPass;
            mail.SendMail(WebConfiguration.MailUsername, txtEmail.Text, WebConfiguration.SiteName + "授权邮件", html);

        }
        /// <summary>
        /// 初始化该成员拥有的角色
        /// </summary>
        /// <param name="staffid"></param>
        private void InitStaffRoles(int staffid)
        {
            IList<StaffRolesInfo> list = new StaffRolesBLL().ListRolesByStaff(staffid);
            if (list != null && list.Count > 0)
                cblRoles.DefaultStaffRole = list;
        }

        /// <summary>
        /// 按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.CheckInput())
            {
                StaffBLL bll = new StaffBLL();
                string orgPass = string.Empty, pass = string.Empty;
                if (hfID.Value.Equals("0") || (!hfID.Value.Equals("0") && (chRandom.Checked || !string.IsNullOrEmpty(txtPassword.Text))))
                {
                    orgPass = chRandom.Checked ? Format.GeneratorRandom(6) : txtPassword.Text.Trim();
                    pass = Security.MD5(orgPass);
                }
                else
                    pass = hfPassword.Value;
                StaffRolesBLL srBll = new StaffRolesBLL();
                if (hfID.Value.Equals("0"))
                {
                    int staffID = bll.InsertStaff(Server.HtmlEncode(txtUsername.Text.Trim()), pass
                        , Server.HtmlEncode(txtEmail.Text.Trim()), chLock.Checked ? "y" : "n", out Message);
                    if (staffID > 0)
                    {
                        srBll.UpdateStaffRoles(staffID, cblRoles.GetSelectValue());
                        this.SendMail(orgPass, pass);
                        base.SetMessage(true, "成员添加成功");
                        base.Redirect("list_staff.aspx?action=list", 1);
                    }
                    else
                        base.SetMessage(false, Message);
                }
                else
                {
                    if (bll.UpdateStaff(int.Parse(hfID.Value), Server.HtmlEncode(txtUsername.Text.Trim()), pass
                        , Server.HtmlEncode(txtEmail.Text.Trim()), chLock.Checked ? "y" : "n", out Message))
                    {
                        srBll.UpdateStaffRoles(int.Parse(hfID.Value), cblRoles.GetSelectValue());
                        this.SendMail(orgPass, pass);
                        base.SetMessage(true, "更新成员成功");
                        base.Redirect("list_staff.aspx?action=list", 1);
                    }
                    else
                        base.SetMessage(false, Message);
                }
            }
        }
        #endregion
    }
}
